package mango.web.server;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

//import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ViewS extends RemoteServiceServlet {
	protected static DataSource ds;

	public DataSource getDs() {
		if (ViewS.ds == null) {

			BasicDataSource ds = new BasicDataSource();

			Property property = Property.get(this.getFilePath("WEB-INF/DB.property"));

			String driverClassName = property.map.get("driverClassName");
			String url = property.map.get("url");
			String username = property.map.get("username");
			String password = property.map.get("password");

			ds.setDriverClassName(driverClassName);

			ds.setUrl(url);
			ds.setUsername(username);
			ds.setPassword(password);

			ViewS.ds = ds;
		}
		return ViewS.ds;
	}

	public QueryRunner newQueryRunner() {
		return new QueryRunner(this.getDs());
	}

	public void close(Statement stmt) {
		try {
			DbUtils.close(stmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void close(Connection conn) {
		try {
			DbUtils.close(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getFilePath(String file) {
		String root = this.getServletContext().getRealPath("/");
		return root + file;
	}
	
	public boolean isEmpty(String str) {
		if (str == null || str.trim().equals("")) return true;
		return false;
	}
}
